package netty.buffer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.junit.Test;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class C02_HeapAndDirect {
    @Test
    public void testHeapBuf() {
        //创建堆内存的缓冲区
        ByteBuf heapBuffer = ByteBufAllocator.DEFAULT.buffer(); //4.1版本的Netty,默认使用池化技术的Allocator分配器
        heapBuffer.writeBytes("heap buffer".getBytes());
        if (heapBuffer.hasArray()) {

            //取得内部数组
            byte[] array = heapBuffer.array();
            int arrayOffset = heapBuffer.arrayOffset();
            int readerIndex = heapBuffer.readerIndex();
            int readableBytes = heapBuffer.readableBytes();
        }
        //释放缓冲区
        heapBuffer.release();
    }
}
